package com.sos.core.game 
{
	import com.sos.core.DataObject;
	import com.sos.core.LogRowType;
	import com.sos.core.Position;
	/**
	 * ...
	 * @author Francesco Forte
	 */
	public class GameLogRow extends DataObject
	{
		
		// -------------------------------------------------------------------------------
		// CONSTANTS
		// -------------------------------------------------------------------------------
		
		protected static const 		JSON_PROPERTY_TYPE:String			= "type";
		protected static const 		JSON_PROPERTY_DATA:String			= "data";
		protected static const 		JSON_PROPERTY_TEXT:String			= "text";
		protected static const 		JSON_PROPERTY_SOURCEPLAYER:String	= "sourcePlayer";
		protected static const 		JSON_PROPERTY_TARGETID:String		= "targetId";
		protected static const 		JSON_PROPERTY_TARGETPOS:String		= "targetPos";
		protected static const 		JSON_PROPERTY_TARGETUSER:String		= "user";
		
		
		// -------------------------------------------------------------------------------
		// FIELDS
		// -------------------------------------------------------------------------------
		
		protected var _type:LogRowType;
		protected var _text:String;
		protected var _sourcePlayer:uint;
		protected var _cast:GameCast;
		protected var _proc:GameProc;
		protected var _targetId:uint;
		protected var _targetPos:Position;
		protected var _targetUser:uint;
		
		// -------------------------------------------------------------------------------
		// CONSTRUCTOR
		// -------------------------------------------------------------------------------
		
		public function GameLogRow(data:Object = null) 
		{
			super();
			
			if (data != null) load(data);
		}
		
		// -------------------------------------------------------------------------------
		// PROPERTIES
		// -------------------------------------------------------------------------------
		
		public function get type():LogRowType
		{
			return _type;
		}
		
		public function get text():String 
		{
			return _text;
		}
		
		public function set text(value:String):void 
		{
			_text = value;
		}
		
		public function get sourcePlayer():uint 
		{
			return _sourcePlayer;
		}
		
		public function set sourcePlayer(value:uint):void 
		{
			_sourcePlayer = value;
		}
		
		public function get cast():GameCast 
		{
			return _cast;
		}
		
		public function set cast(value:GameCast):void 
		{
			_cast = value;
		}
		
		public function get proc():GameProc 
		{
			return _proc;
		}
		
		public function set proc(value:GameProc):void 
		{
			_proc = value;
		}
		
		public function get targetId():uint 
		{
			return _targetId;
		}
		
		public function set targetId(value:uint):void 
		{
			_targetId = value;
		}
		
		public function get targetPos():Position 
		{
			return _targetPos;
		}
		
		public function set targetPos(value:Position):void 
		{
			_targetPos = value;
		}
		
		public function get targetUser():uint 
		{
			return _targetUser;
		}
		
		public function set targetUser(value:uint):void 
		{
			_targetUser = value;
		}

		
		// -------------------------------------------------------------------------------
		// PRIVATE METHODS
		// -------------------------------------------------------------------------------
		
		override protected function validate(data:Object):void
		{
			// Validating data
			if (!data.hasOwnProperty(JSON_PROPERTY_TYPE)) 		throwLoadMissingPropError(JSON_PROPERTY_TYPE);
			
		}
		
		override protected function fill(data:Object):void
		{
			// Load data
			_type 		= LogRowType.getByName(data[JSON_PROPERTY_TYPE]);
			
			if (data.hasOwnProperty(JSON_PROPERTY_DATA)) {
				
				var raw_data:Object = data[JSON_PROPERTY_DATA];
				switch (_type) {
					case LogRowType.DEBUG:
						_text = raw_data[JSON_PROPERTY_TEXT];
						break;
					case LogRowType.DISCARD:
						_text 		= raw_data[JSON_PROPERTY_TEXT];
						_targetUser = raw_data[JSON_PROPERTY_TARGETUSER];
						break;
					case LogRowType.CAST_INIT:
						_text 			= raw_data[JSON_PROPERTY_TEXT];
						_sourcePlayer 	= uint(raw_data[JSON_PROPERTY_SOURCEPLAYER]);
						
						break;
					case LogRowType.CAST:
						_cast = new GameCast(raw_data);
						break;
					case LogRowType.ABILITY:
						_proc = new GameProc(raw_data);
						break;
					case LogRowType.INTERRUPT:
						_proc = new GameProc(raw_data);
						break;
					case LogRowType.SWAP:
						_text = raw_data[JSON_PROPERTY_TEXT];
						_targetId = raw_data[JSON_PROPERTY_TARGETID];
						_targetPos = Position.getBySign(raw_data[JSON_PROPERTY_TARGETPOS]);
						_sourcePlayer = uint(raw_data[JSON_PROPERTY_SOURCEPLAYER]);
						break;
				}
			}
			
			
			
			
		}
		
		// -------------------------------------------------------------------------------
		// PUBLIC METHODS
		// -------------------------------------------------------------------------------
		
		override public function clear():void
		{
			// Clear data
			_text 			= "";
			_type 			= LogRowType.DEBUG;
			_sourcePlayer	= DataObject.INVALID_ID;
			_targetUser		= DataObject.INVALID_ID;
			// Clear objects
			_cast = new GameCast();
			_proc = new GameProc();
		}

		
	}

}